class Solution {//leetcode5——最长回文子串
public:
    string longestPalindrome(string s) {
        int n=s.size();
        vector<vector<int>> dp(n,vector<int>(n,0));
        int max_len=1;
        int begin=0;
        for(int i=n-1;i>=0;i--){
            for(int j=i;j<n;j++){
                if(s[i]==s[j]){
                    if(i==j||i+1==j) dp[i][j]=j-i+1;
                    else if(dp[i+1][j-1]!=0) dp[i][j]=dp[i+1][j-1]+2;
                    if(dp[i][j]>max_len) {
                        max_len=dp[i][j];
                        begin=i;
                    }
                }
            }
        }
        return s.substr(begin,max_len);
    }
};